In [1]:
import pandas as pd

# Pad naar het CSV-bestand
file_path = "/Users/yassir/Downloads/World Happiness Report.csv"

# Lees het CSV-bestand in een DataFrame
df = pd.read_csv(file_path)

# Bekijk de eerste vijf rijen van het DataFrame
print(df.head(n=5))
print(df.columns)
  Country Name Regional Indicator  Year  Life Ladder  Log GDP Per Capita  \
0  Afghanistan         South Asia  2008     3.723590            7.350416   
1  Afghanistan         South Asia  2009     4.401778            7.508646   
2  Afghanistan         South Asia  2010     4.758381            7.613900   
3  Afghanistan         South Asia  2011     3.831719            7.581259   
4  Afghanistan         South Asia  2012     3.782938            7.660506   

   Social Support  Healthy Life Expectancy At Birth  \
0        0.450662                         50.500000   
1        0.552308                         50.799999   
2        0.539075                         51.099998   
3        0.521104                         51.400002   
4        0.520637                         51.700001   

   Freedom To Make Life Choices  Generosity  Perceptions Of Corruption  \
0                      0.718114    0.167652                   0.881686   
1                      0.678896    0.190809                   0.850035   
2                      0.600127    0.121316                   0.706766   
3                      0.495901    0.163571                   0.731109   
4                      0.530935    0.237588                   0.775620   

   Positive Affect  Negative Affect  Confidence In National Government  
0         0.414297         0.258195                           0.612072  
1         0.481421         0.237092                           0.611545  
2         0.516907         0.275324                           0.299357  
3         0.479835         0.267175                           0.307386  
4         0.613513         0.267919                           0.435440  
Index(['Country Name', 'Regional Indicator', 'Year', 'Life Ladder',
       'Log GDP Per Capita', 'Social Support',
       'Healthy Life Expectancy At Birth', 'Freedom To Make Life Choices',
       'Generosity', 'Perceptions Of Corruption', 'Positive Affect',
       'Negative Affect', 'Confidence In National Government'],
      dtype='object')
In [2]:
import pandas as pd

# Pad naar het CSV-bestand
file_path = "/Users/yassir/Downloads/World Happiness Report.csv"

# Lees het CSV-bestand in een DataFrame
df = pd.read_csv(file_path)

# Bekijk het aantal records in het DataFrame
print("Aantal records:", df.shape[0])
Aantal records: 2199
In [3]:
import pandas as pd

# Pad naar het CSV-bestand
file_path = "/Users/yassir/Downloads/World Happiness Report.csv"

# Lees het CSV-bestand in een DataFrame
df = pd.read_csv(file_path)

# Bekijk het aantal variabelen (kolommen) in het DataFrame
print("Aantal variabelen:", df.shape[1])
Aantal variabelen: 13
In [4]:
import pandas as pd

# Pad naar het CSV-bestand
file_path = "/Users/yassir/Downloads/World Happiness Report.csv"

# Lees het CSV-bestand in een DataFrame
df = pd.read_csv(file_path)

# Vind de rij met de hoogste Positive Affect
highest_positive_affect_row = df.nlargest(1, 'Positive Affect')

# Print de rij met de hoogste Positive Affect met de landnaam
print(highest_positive_affect_row[['Country Name', 'Positive Affect']])
     Country Name  Positive Affect
1942     Thailand         0.883586
In [5]:
# Bereken specifieke statistieken voor de variabele "Life Ladder"
life_ladder_median = df['Life Ladder'].median()
life_ladder_range = df['Life Ladder'].max() - df['Life Ladder'].min()
life_ladder_iqr = df['Life Ladder'].quantile(0.75) - df['Life Ladder'].quantile(0.25)

# Print de berekende statistieken
print("Median:", life_ladder_median)
print("Range:", life_ladder_range)
print("Interquartile Range:", life_ladder_iqr)
Median: 5.43243742
Range: 6.7376631499999995
Interquartile Range: 1.6627104284999996
In [6]:
# Bereken statistieken voor de variabele "Life Ladder"
life_ladder_stats = df['Life Ladder'].describe()

# Print de statistieken
print(life_ladder_stats)
count    2199.000000
mean        5.479226
std         1.125529
min         1.281271
25%         4.646750
50%         5.432437
75%         6.309460
max         8.018934
Name: Life Ladder, dtype: float64
In [7]:
# Pad naar het CSV-bestand
file_path = "/Users/yassir/Downloads/Life Expectancy Data.csv"

# Lees het CSV-bestand in een DataFrame
df_lf = pd.read_csv(file_path)

# Bekijk de eerste vijf rijen van het DataFrame
print(df_lf.head(n=5))
print(df_lf.columns)
       Country  Year      Status  Life expectancy   Adult Mortality  \
0  Afghanistan  2015  Developing              65.0            263.0   
1  Afghanistan  2014  Developing              59.9            271.0   
2  Afghanistan  2013  Developing              59.9            268.0   
3  Afghanistan  2012  Developing              59.5            272.0   
4  Afghanistan  2011  Developing              59.2            275.0   

   infant deaths  Alcohol  percentage expenditure  Hepatitis B  Measles   ...  \
0             62     0.01               71.279624         65.0      1154  ...   
1             64     0.01               73.523582         62.0       492  ...   
2             66     0.01               73.219243         64.0       430  ...   
3             69     0.01               78.184215         67.0      2787  ...   
4             71     0.01                7.097109         68.0      3013  ...   

   Polio  Total expenditure  Diphtheria    HIV/AIDS         GDP  Population  \
0    6.0               8.16         65.0        0.1  584.259210  33736494.0   
1   58.0               8.18         62.0        0.1  612.696514    327582.0   
2   62.0               8.13         64.0        0.1  631.744976  31731688.0   
3   67.0               8.52         67.0        0.1  669.959000   3696958.0   
4   68.0               7.87         68.0        0.1   63.537231   2978599.0   

    thinness  1-19 years   thinness 5-9 years  \
0                   17.2                 17.3   
1                   17.5                 17.5   
2                   17.7                 17.7   
3                   17.9                 18.0   
4                   18.2                 18.2   

   Income composition of resources  Schooling  
0                            0.479       10.1  
1                            0.476       10.0  
2                            0.470        9.9  
3                            0.463        9.8  
4                            0.454        9.5  

[5 rows x 22 columns]
Index(['Country', 'Year', 'Status', 'Life expectancy ', 'Adult Mortality',
       'infant deaths', 'Alcohol', 'percentage expenditure', 'Hepatitis B',
       'Measles ', ' BMI ', 'under-five deaths ', 'Polio', 'Total expenditure',
       'Diphtheria ', ' HIV/AIDS', 'GDP', 'Population',
       ' thinness  1-19 years', ' thinness 5-9 years',
       'Income composition of resources', 'Schooling'],
      dtype='object')
In [8]:
print(df_combined.head(5))
print(df_combined.columns)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[8], line 1
----> 1 print(df_combined.head(5))
      2 print(df_combined.columns)

NameError: name 'df_combined' is not defined
In [9]:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt 


# Laad de CSV-bestanden in DataFrames
file_path_happiness = "/Users/yassir/Downloads/World Happiness Report.csv"
file_path_life_expectancy = "/Users/yassir/Downloads/Life Expectancy Data.csv"
df_happiness = pd.read_csv(file_path_happiness)
df_life_expectancy = pd.read_csv(file_path_life_expectancy)

# Controleer de kolommen en pas ze indien nodig aan
print(df_happiness.columns)
print(df_life_expectancy.columns)

# Zorg ervoor dat de kolommen voor de join dezelfde naam hebben
df_happiness.rename(columns={'Country Name': 'Country'}, inplace=True)
df_life_expectancy.rename(columns={'Life expectancy ': 'Life Expectancy'}, inplace=True)

# Voer de join uit op basis van 'Country' en 'Year'
df_combined = pd.merge(df_happiness, df_life_expectancy, on=['Country', 'Year'], how='inner')

# Bekijk de eerste paar rijen van de gecombineerde dataset
print(df_combined.head())
print(df_combined.columns)
Index(['Country Name', 'Regional Indicator', 'Year', 'Life Ladder',
       'Log GDP Per Capita', 'Social Support',
       'Healthy Life Expectancy At Birth', 'Freedom To Make Life Choices',
       'Generosity', 'Perceptions Of Corruption', 'Positive Affect',
       'Negative Affect', 'Confidence In National Government'],
      dtype='object')
Index(['Country', 'Year', 'Status', 'Life expectancy ', 'Adult Mortality',
       'infant deaths', 'Alcohol', 'percentage expenditure', 'Hepatitis B',
       'Measles ', ' BMI ', 'under-five deaths ', 'Polio', 'Total expenditure',
       'Diphtheria ', ' HIV/AIDS', 'GDP', 'Population',
       ' thinness  1-19 years', ' thinness 5-9 years',
       'Income composition of resources', 'Schooling'],
      dtype='object')
       Country Regional Indicator  Year  Life Ladder  Log GDP Per Capita  \
0  Afghanistan         South Asia  2008     3.723590            7.350416   
1  Afghanistan         South Asia  2009     4.401778            7.508646   
2  Afghanistan         South Asia  2010     4.758381            7.613900   
3  Afghanistan         South Asia  2011     3.831719            7.581259   
4  Afghanistan         South Asia  2012     3.782938            7.660506   

   Social Support  Healthy Life Expectancy At Birth  \
0        0.450662                         50.500000   
1        0.552308                         50.799999   
2        0.539075                         51.099998   
3        0.521104                         51.400002   
4        0.520637                         51.700001   

   Freedom To Make Life Choices  Generosity  Perceptions Of Corruption  ...  \
0                      0.718114    0.167652                   0.881686  ...   
1                      0.678896    0.190809                   0.850035  ...   
2                      0.600127    0.121316                   0.706766  ...   
3                      0.495901    0.163571                   0.731109  ...   
4                      0.530935    0.237588                   0.775620  ...   

   Polio  Total expenditure  Diphtheria   HIV/AIDS         GDP  Population  \
0   64.0               8.33         64.0       0.1  373.361116   2729431.0   
1   63.0               9.42         63.0       0.1  445.893298    284331.0   
2   66.0               9.20         66.0       0.1  553.328940   2883167.0   
3   68.0               7.87         68.0       0.1   63.537231   2978599.0   
4   67.0               8.52         67.0       0.1  669.959000   3696958.0   

    thinness  1-19 years   thinness 5-9 years  \
0                   18.8                 18.9   
1                   18.6                 18.7   
2                   18.4                 18.4   
3                   18.2                 18.2   
4                   17.9                 18.0   

   Income composition of resources  Schooling  
0                            0.433        8.7  
1                            0.434        8.9  
2                            0.448        9.2  
3                            0.454        9.5  
4                            0.463        9.8  

[5 rows x 33 columns]
Index(['Country', 'Regional Indicator', 'Year', 'Life Ladder',
       'Log GDP Per Capita', 'Social Support',
       'Healthy Life Expectancy At Birth', 'Freedom To Make Life Choices',
       'Generosity', 'Perceptions Of Corruption', 'Positive Affect',
       'Negative Affect', 'Confidence In National Government', 'Status',
       'Life Expectancy', 'Adult Mortality', 'infant deaths', 'Alcohol',
       'percentage expenditure', 'Hepatitis B', 'Measles ', ' BMI ',
       'under-five deaths ', 'Polio', 'Total expenditure', 'Diphtheria ',
       ' HIV/AIDS', 'GDP', 'Population', ' thinness  1-19 years',
       ' thinness 5-9 years', 'Income composition of resources', 'Schooling'],
      dtype='object')
In [10]:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
fig1 = px.scatter(df_combined, x='Positive Affect', y='Life Expectancy', title='Positive Affect vs Life Expectancy')
fig2 = px.scatter(df_combined, x='Social Support', y='Life Expectancy', title='Social Support vs Life Expectancy')

fig = go.Figure(data=fig1.data + fig2.data)

fig.update_layout(
    updatemenus=[
        {
            'buttons': [
                {'label': 'Positive Affect', 'method': 'update', 'args': [{'visible': [True, False]}]},
                {'label': 'Social Support', 'method': 'update', 'args': [{'visible': [False, True]}]},
            ],
            'direction': 'down',
            'showactive': True,
        }
    ],
    title="Life Expectancy vs Positive Affect and Social Support"
)

fig.show()
In [11]:
fig = px.scatter(df_combined, x='Freedom To Make Life Choices', y='Life Expectancy', size='Life Expectancy',
                 color='Perceptions Of Corruption', animation_frame='Year', title='Freedom To Make Life Choices vs Life Expectancy Over Time')

fig.show()
In [12]:
corr = df_combined[['Life Expectancy', 'Positive Affect', 'Social Support', 'Freedom To Make Life Choices', 'Perceptions Of Corruption', 'GDP', 'Total expenditure', ' BMI ', 'Alcohol']].corr()

fig = px.imshow(corr, text_auto=True, title='Correlation Matrix')

fig.show()
In [13]:
countries = df_combined['Country'].unique()

fig = go.Figure()

for country in countries:
    country_data = df_combined[df_combined['Country'] == country]
    fig.add_trace(go.Scatter(x=country_data['Year'], y=country_data['Life Expectancy'], mode='lines', name=country))

fig.update_layout(
    title='Life Expectancy Over Time',
    xaxis_title='Year',
    yaxis_title='Life Expectancy',
    updatemenus=[
        {
            'buttons': [{'label': country, 'method': 'update', 'args': [{'visible': [country in countries]}]} for country in countries],
            'direction': 'down',
            'showactive': True,
        }
    ]
)

fig.show()
In [14]:
fig1 = px.bar(df_combined, x='Country', y='GDP', title='GDP per Country')
fig2 = px.bar(df_combined, x='Country', y='Total expenditure', title='Total Expenditure per Country')

fig = go.Figure(data=fig1.data + fig2.data)

fig.update_layout(
    updatemenus=[
        {
            'buttons': [
                {'label': 'GDP', 'method': 'update', 'args': [{'visible': [True, False]}]},
                {'label': 'Total Expenditure', 'method': 'update', 'args': [{'visible': [False, True]}]},
            ],
            'direction': 'down',
            'showactive': True,
        }
    ],
    title="GDP and Total Expenditure per Country"
)

fig.show()
In [15]:
fig = px.scatter_matrix(df_combined, dimensions=['Positive Affect', 'Social Support', 'Freedom To Make Life Choices', 'Perceptions Of Corruption', 'GDP', 'Total expenditure', 'Life Expectancy'],
                        title='Scatter Plot Matrix of Key Variables')

fig.show()
/Users/yassir/anaconda3/lib/python3.11/site-packages/plotly/express/_core.py:279: FutureWarning:

iteritems is deprecated and will be removed in a future version. Use .items instead.

Argument 1: Positieve Affect en Social Support zijn indicatoren van blijdschap en welzijn, wat kan bijdragen aan een hogere levensverwachting. Scatter Plot met Dropdown Menu (Positive Affect vs Life Expectancy, Social Support vs Life Expectancy):

Deze visualisatie laat de relatie zien tussen 'Positive Affect', 'Social Support' en 'Life Expectancy'. Het stelt ons in staat om te observeren of hogere niveaus van positief affect en sociale steun correleren met een hogere levensverwachting. Bubble Chart met Timeline Slider (Freedom To Make Life Choices vs Life Expectancy):

Deze interactieve bubble chart toont de impact van 'Freedom To Make Life Choices' op 'Life Expectancy' over tijd. Het laat zien hoe landen met meer individuele vrijheid vaak een hogere levensverwachting hebben, wat indirect kan worden verbonden met hogere niveaus van blijdschap en welzijn. Argument 2: Vrijheid om levenskeuzes te maken en perceptie van lage corruptie zijn indicatoren van een gelukkigere samenleving, wat kan bijdragen aan een hogere levensverwachting. Scatter Plot Matrix: Deze matrix biedt een overzicht van de relaties tussen 'Freedom To Make Life Choices', 'Perceptions Of Corruption', 'GDP', 'Total expenditure', en 'Life Expectancy'. Het stelt ons in staat om te zien hoe landen met hogere vrijheid en lagere percepties van corruptie vaak betere gezondheids- en welzijnsresultaten hebben, wat weer kan bijdragen aan een hogere levensverwachting. Tegenargumenten: Bar Chart met Dropdown Menu (GDP per Country, Total Expenditure per Country): Door GDP en Total Expenditure per land te vergelijken in afzonderlijke bar charts, kunnen we de correlatie tussen deze economische factoren en levensverwachting onderzoeken. Dit helpt ons te begrijpen of sociaaleconomische factoren mogelijk een grotere rol spelen dan geluksniveaus bij het bepalen van levensverwachting. Deze visualisaties bieden een gevarieerde set tools om de verbanden te onderzoeken tussen geluk, welzijn, sociaaleconomische factoren en levensverwachting. Ze helpen bij het illustreren van de complexe relaties die kunnen bestaan tussen deze variabelen, en bieden een interactieve ervaring om dieper in de data te duiken en trends over tijd te begrijpen.

Argumenten 1 en 2:¶

De scatter plot matrix kan gebruikt worden om de relaties tussen verschillende variabelen te visualiseren, waaronder 'Positive Affect', 'Social Support', 'Freedom To Make Life Choices', 'Perceptions Of Corruption', 'GDP', 'Total expenditure', en 'Life Expectancy'. Hier is hoe het specifiek kan bijdragen aan de ondersteuning van onze argumenten:

  1. Visualisatie van Verbanden:

    • De scatter plot matrix maakt het mogelijk om tegelijkertijd meerdere variabelen te visualiseren. Hierdoor kunnen we directe verbanden observeren tussen 'Positive Affect' en 'Life Expectancy', 'Social Support' en 'Life Expectancy', evenals 'Freedom To Make Life Choices' en 'Perceptions Of Corruption' in relatie tot 'Life Expectancy'. Dit stelt ons in staat om te zien of er een duidelijk patroon is waarbij hogere niveaus van positief affect, sociale steun, individuele vrijheid en lagere corruptiepercepties samengaan met een hogere levensverwachting.
  2. Identificeren van Correlaties:

    • Door de matrix te bestuderen, kunnen we corrigerende patronen ontdekken tussen deze variabelen. Bijvoorbeeld, als landen met hogere 'Freedom To Make Life Choices' vaak ook hogere 'Positive Affect' scores en een hogere 'Life Expectancy' hebben, versterkt dit het argument dat vrijheid van keuze kan bijdragen aan een gelukkigere en gezondere bevolking.
  3. Multivariate Analyse:

    • De scatter plot matrix is een vorm van multivariate analyse, wat betekent dat het ons in staat stelt om meerdere variabelen tegelijkertijd te onderzoeken. Dit is cruciaal om complexe relaties te begrijpen en om te zien hoe verschillende factoren samenwerken om de levensverwachting te beïnvloeden.

Door gebruik te maken van deze visualisatie kunnen we dieper ingaan op de verbanden tussen geluk, welzijn, sociaaleconomische factoren en levensverwachting. Het helpt om patronen en trends te identificeren die moeilijker te zien zijn in afzonderlijke plots, waardoor een meer holistisch begrip ontstaat van de onderliggende mechanismen die bijdragen aan een hogere levensverwachting in verschillende landen